package mvc.controller;

import mvc.config.MyBatisConfig;
import org.apache.commons.mail.HtmlEmail;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;


@Controller
public class EmailRegController {
    private String captcha;

    @RequestMapping("/regNow.do")
    public ModelAndView reg(){
        return new ModelAndView("reg");
    }
    @RequestMapping("/loginNow.do")
    public ModelAndView login(){ return new ModelAndView("login");}

    @RequestMapping(value = "/regemail.do",produces = "text/html;charset=utf-8")
    @ResponseBody
    public void start(String mailIndex) {
        try {
            System.out.println(mailIndex);
            HtmlEmail htmlEmail=new HtmlEmail();
            htmlEmail.setHostName("smtp.qq.com");
            htmlEmail.setCharset("utf-8");
            htmlEmail.addTo(mailIndex);//目标邮箱地址
            htmlEmail.setFrom("maoxu1979@qq.com","邮箱验证");//你的邮箱地址
            htmlEmail.setAuthentication("maoxu1979@qq.com","bplpoonvoecbfebj");//你的邮箱地址和你的stmp密码
            int a = (int)((Math.random()*9+1)*100000);
            captcha = String.valueOf(a);
            System.out.println(captcha);
            htmlEmail.setTextMsg("验证码");
            htmlEmail.setMsg("<!DOCTYPE html>\n"+
                    "<html lang='en'>\n"+
                    "<head>\n"+
                    "<meta charset='UTF-8'>\n"+
                    "<title>邮箱验证</title>\n"+
                    "</head>\n"+
                    "<body>\n"+
                    "<h3>亲爱的用户:</h3>\n"+
                    "<P>您好！您正在进行邮箱验证，本次请求的验证码为:"+
                    "<span style='color: #ff7518;font-size: 20px;font-weight: 600'>"+captcha+"</span>"+
                    "(为了保障您帐号的安全性,请在10分钟内完成验证)</P>\n"+
                    "</body>\n"+
                    "</html>\n");//内容最好不要太简单了，不然容易进垃圾邮箱
            htmlEmail.send();
            System.out.println("发送成功");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/checkreg.do")
    public ModelAndView checkreg(String username,String password2,String mailIndex,String captcha, ModelMap map) {
        ModelAndView mav = new ModelAndView();
        System.out.println("checkreg验证开始");
        try {
            String a = this.captcha;
            System.out.println(captcha);
            System.out.println(a);

            if (captcha.equals(a)){
                System.out.println("验证码正确");

                System.out.println("注册开始");
                System.out.println(username+","+password2+","+mailIndex);
                AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyBatisConfig.class);
                /*UserMapper um = ctx.getBean("userMapper",UserMapper.class);
                User user = new User(0,username,password2,mailIndex);
                Integer sum = um.addUserWithId(user);
                if ("1".equals(sum)){
                    System.out.println("注册成功");
                    return new ModelAndView("reg");
                }*/
               DataSource dataSource = ctx.getBean("dataSource",DataSource.class);
                try(Connection conn = dataSource.getConnection()) {
                    String sql = "insert into user values(null,?,?,?)";
                    PreparedStatement ps = conn.prepareStatement(sql);
                    ps.setString(1, username);
                    ps.setString(2, password2);
                    ps.setString(3,mailIndex);
                    ps.executeUpdate();
                }catch (Exception e){
                    e.printStackTrace();
                }
                System.out.println("注册成功");
                return new ModelAndView("login");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("txt","验证码错误");
        System.out.println("验证码错误");
        return new ModelAndView("reg");
    }
}
